monthlyPublishedVariantsReport

- 🇬🇧 English
- 🇮🇹 Italiano
Function Names: monthlyPublishedVariantsReport / testMonthlyPublishedVariantsReport
Trigger: Scheduler (onSchedule - every Friday at 17:50) / HTTPS (onRequest)
Purpose: Executes a weekly notification (every Friday at 17:50) to monitor SKUs in 'In Pubblicazione' and 'Rev. Cliente' status grouped by brand and batch. Sends summary emails only to Validators and Clients associated with specific brands, avoiding generic notifications to the entire system.
Detailed Functionality​
This Firebase Function performs the following operations in sequence:
1. MainBrands Data Collection​
- Retrieves all brands from the
MainBrandscollection in the database - For each brand, processes all associated variants to count status occurrences
2. Variants Grouping and Counting​
- For each brand, groups
Variantsby batch field - Counts variants with status 'In Pubblicazione' (
skuPublication) - Counts variants with status 'Rev. Cliente' (
skuRevClient) - Creates brand/batch combinations for reporting
3. Profile Filtering and Brand Association​
- Retrieves only profiles with role 'Validator' and 'Cliente' from
Profilescollection - Filters profiles by brand using the
list_main_brandsfield - Associates each user with their specific brands for targeted notifications
4. Conditional Email Sending Logic​
Emails are sent ONLY if:
- There are SKUs to report (
skuPublication> 0 ORskuRevClient> 0) - There are recipients for that specific brand
- Brand has associated Validators or Clients in their
list_main_brands
5. Email Configuration and Recipients​
- Primary Recipients (TO): Validators who have the specific brand in their
list_main_brands - Copy Recipients (CC): Clients who have the specific brand in their
list_main_brands - Email Service: ZeptoMail with predefined template (
MAIL_TEMPLATE_KEY: 13ef.8598f19fbcc5adb.k1.afbab030-45ff-11f0-832a-66e0c45c7bae.1975a0d02b3) - Fallback: Nodemailer for error cases
6. Template Data Variables​
The email template receives these merge variables:
brandName: Name of the brandbatch: Batch identifierskuPublication: Number of SKUs in 'In Pubblicazione' statusskuRevClient: Number of SKUs in 'Rev. Cliente' status
7. Automatic Scheduling​
- Schedule: Every Friday at 17:50 CET (
50 17 * * 5) - Region: europe-central2
- Timezone: Europe/Rome
8. Testing Function​
- HTTP Endpoint:
testMonthlyPublishedVariantsReport - Method: GET (no parameters required)
- Purpose: Manual testing with detailed JSON response
- Behavior: Identical logic but returns comprehensive debugging information
Input (Payload):​
For monthlyPublishedVariantsReport (Scheduled):
Automatic execution - no input required
For testMonthlyPublishedVariantsReport (HTTP):
Method: GET
No body parameters required
Output (Success):​
{
"success": true,
"message": "🧪 Report TEST generato con successo",
"timestamp": "2024-01-19T16:50:00.000Z",
"statistics": {
"totalBrands": 5,
"totalBrandBatchCombinations": 12,
"brandsWithSkuToReport": 8,
"totalProfiles": {
"validators": 3,
"clients": 2
},
"emailResults": {
"sent": 6,
"skipped": 2,
"errors": 0
}
},
"results": [
{
"brandId": "brand123",
"brandName": "Ray-Ban",
"batch": "2024-Q1",
"skuPublication": 15,
"skuRevClient": 3
}
],
"profilesByBrand": {
"brand123": {
"validators": ["validator1@example.com"],
"clients": ["client1@example.com"]
}
},
"emailResults": [
{
"brand": "Ray-Ban",
"batch": "2024-Q1",
"status": "sent",
"skuPublication": 15,
"skuRevClient": 3,
"recipients": {
"to": 1,
"cc": 1,
"validators": ["validator1@example.com"],
"clients": ["client1@example.com"]
}
}
]
}
This output is returned in the HTTP response (Postman, browser, curl)
Testing​
URL (if HTTPS): http://127.0.0.1:5001/arshadesstaging/europe-central2/testMonthlyPublishedVariantsReport
Test with Emulator:
- Start the Firebase emulator:
firebase emulators:start --only functions - Ensure you're using Node.js version 20:
nvm use 20 - Test with curl:
curl "http://127.0.0.1:5001/arshadesstaging/europe-central2/testMonthlyPublishedVariantsReport"
Postman Testing:
- Method: GET
- URL: http://127.0.0.1:5001/arshadesstaging/europe-central2/testMonthlyPublishedVariantsReport
- Headers: None required
- Body: Not required
Deploy Command:​
firebase deploy --only functions:monthlyPublishedVariantsReport,functions:testMonthlyPublishedVariantsReport
Production URL:​
monthlyPublishedVariantsReport: https://europe-central2-arshades-7e18a.cloudfunctions.net/monthlyPublishedVariantsReport
Note: This function runs automatically every Friday at 17:50. The URL is for reference only as scheduled functions cannot be called directly.
testMonthlyPublishedVariantsReport: Not deployed to production (test function only)
Function Names: monthlyPublishedVariantsReport / testMonthlyPublishedVariantsReport
Trigger: Scheduler (onSchedule - ogni venerdì alle 17:50) / HTTPS (onRequest)
Purpose: Esegue una notifica settimanale (ogni venerdì alle 17:50) per monitorare gli SKU in stato 'In Pubblicazione' e 'Rev. Cliente' raggruppati per brand e batch. Invia email di riepilogo solo ai Validator e Clienti associati a specifici brand, evitando notifiche generiche a tutto il sistema.
Funzionamento Dettagliato​
Questa Firebase Function esegue le seguenti operazioni in sequenza:
1. Raccolta Dati MainBrands​
- Recupera tutti i brand dalla collezione
MainBrandsnel database - Per ogni brand, elabora tutte le varianti associate per contare le occorrenze di stato
2. Raggruppamento e Conteggio Varianti​
- Per ogni brand, raggruppa le
Variantsper campo batch - Conta le varianti con stato 'In Pubblicazione' (
skuPublication) - Conta le varianti con stato 'Rev. Cliente' (
skuRevClient) - Crea combinazioni brand/batch per il reporting
3. Filtro Profili e Associazione Brand​
- Recupera solo i profili con ruolo 'Validator' e 'Cliente' dalla collezione
Profiles - Filtra i profili per brand utilizzando il campo
list_main_brands - Associa ogni utente ai suoi brand specifici per notifiche mirate
4. Logica di Invio Email Condizionale​
Le email vengono inviate SOLO se:
- Ci sono SKU da segnalare (
skuPublication> 0 OskuRevClient> 0) - Ci sono destinatari per quel brand specifico
- Il brand ha Validator o Clienti associati nel loro
list_main_brands
5. Configurazione Email e Destinatari​
- Destinatari Principali (TO): Validator che hanno il brand specifico nel loro
list_main_brands - Destinatari in Copia (CC): Clienti che hanno il brand specifico nel loro
list_main_brands - Servizio Email: ZeptoMail con template predefinito (
MAIL_TEMPLATE_KEY: 13ef.8598f19fbcc5adb.k1.afbab030-45ff-11f0-832a-66e0c45c7bae.1975a0d02b3) - Fallback: Nodemailer per casi di errore
6. Variabili Dati Template​
Il template email riceve queste variabili di merge:
brandName: Nome del brandbatch: Identificatore del batchskuPublication: Numero di SKU in stato 'In Pubblicazione'skuRevClient: Numero di SKU in stato 'Rev. Cliente'
7. Pianificazione Automatica​
- Programma: Ogni venerdì alle 17:50 CET (
50 17 * * 5) - Regione: europe-central2
- Fuso Orario: Europe/Rome
8. Funzione di Test​
- Endpoint HTTP:
testMonthlyPublishedVariantsReport - Metodo: GET (nessun parametro richiesto)
- Scopo: Test manuale con risposta JSON dettagliata
- Comportamento: Logica identica ma restituisce informazioni complete di debugging
Input (Payload):​
Per monthlyPublishedVariantsReport (Schedulata):
Esecuzione automatica - nessun input richiesto
Per testMonthlyPublishedVariantsReport (HTTP):
Metodo: GET
Nessun parametro nel body richiesto
Output (Success):​
{
"success": true,
"message": "🧪 Report TEST generato con successo",
"timestamp": "2024-01-19T16:50:00.000Z",
"statistics": {
"totalBrands": 5,
"totalBrandBatchCombinations": 12,
"brandsWithSkuToReport": 8,
"totalProfiles": {
"validators": 3,
"clients": 2
},
"emailResults": {
"sent": 6,
"skipped": 2,
"errors": 0
}
},
"results": [
{
"brandId": "brand123",
"brandName": "Ray-Ban",
"batch": "2024-Q1",
"skuPublication": 15,
"skuRevClient": 3
}
],
"profilesByBrand": {
"brand123": {
"validators": ["validator1@example.com"],
"clients": ["client1@example.com"]
}
},
"emailResults": [
{
"brand": "Ray-Ban",
"batch": "2024-Q1",
"status": "sent",
"skuPublication": 15,
"skuRevClient": 3,
"recipients": {
"to": 1,
"cc": 1,
"validators": ["validator1@example.com"],
"clients": ["client1@example.com"]
}
}
]
}
Questo output viene restituito nella risposta HTTP (Postman, browser, curl)
Testing​
URL (if HTTPS): http://127.0.0.1:5001/arshadesstaging/europe-central2/testMonthlyPublishedVariantsReport
Test con Emulator:
- Avviare l'emulatore Firebase:
firebase emulators:start --only functions - Assicurarsi di usare Node.js versione 20:
nvm use 20 - Testare con curl:
curl "http://127.0.0.1:5001/arshadesstaging/europe-central2/testMonthlyPublishedVariantsReport"
Postman Testing:
- Metodo: GET
- URL: http://127.0.0.1:5001/arshadesstaging/europe-central2/testMonthlyPublishedVariantsReport
- Headers: Nessuno richiesto
- Body: Non richiesto
Deploy Command:​
firebase deploy --only functions:monthlyPublishedVariantsReport,functions:testMonthlyPublishedVariantsReport
URL di Produzione:​
monthlyPublishedVariantsReport: https://europe-central2-arshades-7e18a.cloudfunctions.net/monthlyPublishedVariantsReport
Nota: Questa funzione viene eseguita automaticamente ogni venerdì alle 17:50. L'URL è solo di riferimento poiché le funzioni schedulate non possono essere chiamate direttamente.
testMonthlyPublishedVariantsReport: Non deployata in produzione (solo funzione di test)